﻿using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Interactivity;
using DialogHostAvalonia;
using Klear.Library.Models;
using Klear.Library.ViewModels;

namespace Klear.Views;

public partial class CategoryView : UserControl
{
    
    public CategoryView() {
        InitializeComponent();
    }

    

    // 为了修改错误，暂时将DialogClosingEventArgs改成了RoutedEventArgs
    private void OnCloseEditDialog(object? sender, DialogClosingEventArgs e) {
        // 处理弹窗关闭时的操作
    }
    
    public static readonly DirectProperty<DialogHost, ICommand> CloseDialogCommandProperty =
        AvaloniaProperty.RegisterDirect<DialogHost, ICommand>(
            nameof(CloseDialogCommand),
            o => o.CloseDialogCommand,
            defaultBindingMode: BindingMode.TwoWay);

    private ICommand _closeDialogCommand;
    
    public ICommand CloseDialogCommand {
        get => _closeDialogCommand;
        private set => SetAndRaise<ICommand>(CloseDialogCommandProperty, ref _closeDialogCommand, value);
    }

    private async void OpenEditExpenseCategoryDialog(object? sender, RoutedEventArgs e) {
        await DialogHost.Show(Resources["EditExpenseCategoryDialog"], "CategoryViewDialog");
    }


    private async void OpenAddExpenseCategoryDialog(object? sender, RoutedEventArgs e) {
        AddExpenseCategoryTextBox.Text = "";
        await DialogHost.Show(Resources["AddExpenseCategoryDialog"], "CategoryViewDialog");
    }

    private async void OpenDeleteExpenseCategoryDialog(object? sender, RoutedEventArgs e) {
        await DialogHost.Show(Resources["DeleteExpenseCategoryDialog"], "CategoryViewDialog");
    }
    
    private async void OpenEditIncomeCategoryDialog(object? sender, RoutedEventArgs e) {
        await DialogHost.Show(Resources["EditIncomeCategoryDialog"], "CategoryViewDialog");
    }


    private async void OpenAddIncomeCategoryDialog(object? sender, RoutedEventArgs e) {
        AddIncomeCategoryTextBox.Text = "";
        await DialogHost.Show(Resources["AddIncomeCategoryDialog"], "CategoryViewDialog");
    }

    private async void OpenDeleteIncomeCategoryDialog(object? sender, RoutedEventArgs e) {
        await DialogHost.Show(Resources["DeleteIncomeCategoryDialog"], "CategoryViewDialog");
    }
    

    private void SaveAddExpenseCategory(object? sender, RoutedEventArgs e) {

        if (AddExpenseCategoryTextBlock.Text == null || AddExpenseCategoryTextBox.Text == null) {
            return;
        }

        if (AddExpenseCategoryTextBox.Text.Equals("")) {
            return;
        }
        string expenseCategoryName = AddExpenseCategoryTextBox.Text;
        CategoryType categoryType = CategoryType.Expense;
        CategoryViewModel categoryViewModel = DataContext as CategoryViewModel;
        categoryViewModel.SaveAdd(expenseCategoryName, categoryType);
    }

    private void SaveEditExpenseCategory(object? sender, RoutedEventArgs e) {
        if (EdidExpenseCategoryTextBlock.Text == null || EditExpenseCategoryTextBox.Text == null) {
            return;
        }

        if (EditExpenseCategoryTextBox.Text.Equals("")) {
            return;
        }

        // int id = int.Parse(ExpenseCategoryToEditId.Text);
        string expenseCategoryName = EditExpenseCategoryTextBox.Text;
        CategoryType categoryType = CategoryType.Expense;
        CategoryViewModel categoryViewModel = DataContext as CategoryViewModel;
        categoryViewModel.SaveEdit(expenseCategoryName, categoryType);
        
    }

    private void DoDeleteExpenseCategory(object? sender, RoutedEventArgs e) {
        CategoryViewModel categoryViewModel = DataContext as CategoryViewModel;
        
        // if (CategoryToDeleteId.Text == null) {
        //     return;
        // }
        // int id = int.Parse(CategoryToDeleteId.Text);
        categoryViewModel.DoDelete(categoryViewModel.CategoryToEditId);
    }

    private void SaveAddIncomeCategory(object? sender, RoutedEventArgs e) {
        if (AddIncomeCategoryTextBlock.Text == null || AddIncomeCategoryTextBox.Text == null) {
            return;
        }
        if (AddIncomeCategoryTextBox.Text.Equals("")) {
            return;
        }
        string incomeCategoryName = AddIncomeCategoryTextBox.Text;
        CategoryType categoryType = CategoryType.Income;
        CategoryViewModel categoryViewModel = DataContext as CategoryViewModel;
        categoryViewModel.SaveAdd(incomeCategoryName, categoryType);
    }
    
    private void SaveEditIncomeCategory(object? sender, RoutedEventArgs e) {
        if (EditIncomeCategoryTextBlock.Text == null || EditIncomeCategoryTextBox.Text == null) {
            return;
        }

        if (EditIncomeCategoryTextBox.Text.Equals("")) {
            return;
        }

        // int id = int.Parse(IncomeCategoryToEditId.Text);
        string incomeCategoryName = EditIncomeCategoryTextBox.Text;
        CategoryType categoryType = CategoryType.Income;
        CategoryViewModel categoryViewModel = DataContext as CategoryViewModel;
        categoryViewModel.SaveEdit(incomeCategoryName, categoryType);
        
    }

    private void DoDeleteIncomeCategory(object? sender, RoutedEventArgs e) {
        CategoryViewModel categoryViewModel = DataContext as CategoryViewModel;
        
        // if (CategoryToDeleteId.Text == null) {
        //     return;
        // }
        // int id = int.Parse(CategoryToDeleteId.Text);
        categoryViewModel.DoDelete(categoryViewModel.CategoryToEditId);
    }
}